﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Crawer
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                string url = $"https://xa.fang.lianjia.com/loupan/pg{i}/";
                var html = HtmlHelper.DownloadUrl(url);
                HtmlDocument htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(html);
                {
                    //string path = "/html/body/div[3]/ul[2]/li[1]/div/div[1]/a";//楼盘
                    //HtmlNode node = htmlDocument.DocumentNode.SelectSingleNode(path);

                    //var name = node.InnerHtml;
                    //string PathMainji = "/html/body/div[3]/ul[2]/li[1]/div/div[3]/span";//建筑面积
                    //HtmlNode Mianji = htmlDocument.DocumentNode.SelectSingleNode(PathMainji);
                    //var mianji = Mianji.InnerHtml;
                    //string danjian = "/html/body/div[3]/ul[2]/li[1]/div/div[6]/div[1]/span[1]";//单价
                    //HtmlNode danjia = htmlDocument.DocumentNode.SelectSingleNode(danjian);
                    //var danjia2 = danjia.InnerHtml;
                    //string path3add = "/html/body/div[3]/ul[2]/li[1]/div/div[2]/a";//地址
                    //HtmlNode diz = htmlDocument.DocumentNode.SelectSingleNode(path3add);
                    //var diz1 = diz.InnerHtml;
                    //string path4 = "/html/body/div[3]/ul[2]/li[1]/div/div[1]/span[2]";//动态
                    //HtmlNode dongtai = htmlDocument.DocumentNode.SelectSingleNode(path4);
                    //var dogtai = dongtai.InnerHtml;
                    //var c = "/html/body/div[3]/ul[2]/li[1]";
                    ////所有/html/body/div[3]/ul[2]
                }
                {
                    var pathall = "/html/body/div[3]/ul[2]/li";
                    HtmlNodeCollection node2 = htmlDocument.DocumentNode.SelectNodes(pathall);
                    int c = 0;
                    foreach (var item in node2)
                    {

                        c = ++c;

                        string PathMainji = "/html/body/div[3]/ul[2]/li["+ c + "]/div/div[3]/span";//建筑面积
                                                                                            //HtmlNode Mianji = htmlDocument.DocumentNode.SelectSingleNode(PathMainji);
                                                                                            //var mianji = Mianji.InnerHtml;
                  string danjian = "/html/body/div[3]/ul[2]/li["+ c + "]/div/div[6]/div[1]/span[1]";//单价
                                                                                                   //HtmlNode danjia = htmlDocument.DocumentNode.SelectSingleNode(danjian);
                                                                                                   //var danjia2 = danjia.InnerHtml;
                        string path3add = "/html/body/div[3]/ul[2]/li["+ c + "]/div/div[2]/a";//地址
                                                                                       //HtmlNode diz = htmlDocument.DocumentNode.SelectSingleNode(path3add);
                                                                                       //var diz1 = diz.InnerHtml;
                        string path4 = "/html/body/div[3]/ul[2]/li["+ c + "]/div/div[1]/span[2]";//动态
                                                                                                 //HtmlNode dongtai = htmlDocument.DocumentNode.SelectSingleNode(path4);
                                                                                                 //var dogtai = dongtai.InnerHtml;
                        string path5 = "/html/body/div[3]/ul[2]/li[" + c + "]/div/div[1]/a";//楼盘
                        var item5 = item.SelectSingleNode(path5);//楼盘
                        var item1 = item.SelectSingleNode(PathMainji);//建筑面积
                        var item2 = item.SelectSingleNode(danjian);//单价
                        var item3 = item.SelectSingleNode(path3add);//地址
                        var item4 = item.SelectSingleNode(path4);//动态
                        var mianji = item1.InnerHtml;
                        var danjia = item2.InnerHtml;
                        var diz = item3.InnerHtml;
                        var dongtai = item4.InnerHtml;
                        var loupan = item5.InnerHtml;

                        Console.WriteLine("loupan{0}weizi{1}jianmian{2}danjia{3}dongtai{4}", loupan, diz, mianji, danjia, dongtai);
                        
                    }

                    Console.WriteLine("*********************************************");
                    Console.WriteLine("*********************************************");
                    Console.WriteLine("*********************************************");
                }
            }

            Console.ReadKey();
            //多个
            //  HtmlNodeCollection nodes = htmlDocument.DocumentNode.SelectNodes(path);
            //x path   / html / body / div[3] / ul[2] / li[2] / a
            //分页问题https://xa.fang.lianjia.com/loupan/pg100/ 

        }
    }
}
